package data.store;

import org.xerial.snappy.SnappyOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

//lineItem表的存储空间
//与part表的存储结构一样
public class LineItemSpace {

    private Node head;
    private Node tail;
    private int count;

    public void add(byte b) {
        if(count == 524288){
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(262144);
            SnappyOutputStream snappyOutputStream = new SnappyOutputStream(outputStream);
            try {
                snappyOutputStream.write(tail.data, 0, 524288);
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] compressed = outputStream.toByteArray();
            tail.data = compressed;
            Node node = new Node();
            tail.next = node;
            tail = node;
            count = 0;
        }
        if(head == null){
            head = new Node();
            tail = head;
            count = 0;
        }
        tail.data[count++] = b;
    }

    public boolean isStoringData(){
        return head == null ? false : true;
    }

    static class Node{
        private byte[] data;
        private Node next;

        Node(){
            data = new byte[524288]; //512KB
            next = null;
        }
    }

}
